package com.ruoyi.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.domain.UcArea;
import com.ruoyi.web.domain.vo.QueryVo;
import com.ruoyi.web.mapper.UcAreaMapper;
import com.ruoyi.web.service.IUcAreaService;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cpic
 * @since 2023-01-13
 */
@Service
public class UcAreaServiceImpl extends ServiceImpl<UcAreaMapper, UcArea> implements IUcAreaService {


    @Override
    public List<UcArea> listBy(QueryVo queryVo) {
        QueryWrapper<UcArea> qw =new QueryWrapper<>();
        if(!StringUtils.isEmpty(queryVo.getQKey())){
            qw.like("code",queryVo.getQKey());
            qw.or().like("name",queryVo.getQKey());
        }
        qw.orderByAsc("sequence");
        return baseMapper.selectList(qw);
    }

    @Override
    public Boolean repeatCheck(UcArea ucArea) {
        Boolean ret = false;

        QueryWrapper<UcArea> qw=new QueryWrapper<>();
        qw.eq("code",ucArea.getCode());
        qw.or().eq("parent_id",ucArea.getParentId()).eq("name",ucArea.getName());
        UcArea oldUcArea = baseMapper.selectOne(qw);
        if(oldUcArea==null || oldUcArea.getId().equals(ucArea.getId())){
            ret = true;
        }

        return ret;
    }

    @Override
    public int insert(UcArea ucArea) {

        UcArea ucAreaParent = baseMapper.selectById(ucArea.getParentId());
        if(ucAreaParent==null){
            ucArea.setLevel(1);
            ucArea.setFullName(ucArea.getName());
        }else{
            ucArea.setLevel(ucAreaParent.getLevel()+1);
            ucArea.setFullName(ucAreaParent.getFullName()+ucArea.getName());
        }
        ucArea.setDelStatus(1);
        baseMapper.insert(ucArea);

        if(ucAreaParent==null){
            ucArea.setLevel(1);
            ucArea.setFullName(ucArea.getName());
            ucArea.setIdPath(ucArea.getId()+";");
        }else{
            ucArea.setLevel(ucAreaParent.getLevel()+1);
            ucArea.setFullName(ucAreaParent.getFullName()+ucArea.getName());
            ucArea.setIdPath(ucAreaParent.getIdPath()+ucArea.getId()+";");
        }

        return baseMapper.updateById(ucArea);
    }

    @Override
    public int update(UcArea ucArea) {

        UcArea olducArea = baseMapper.selectById(ucArea.getId());

        UcArea ucAreaParent = baseMapper.selectById(ucArea.getParentId());

        if(ucAreaParent==null){
            ucArea.setLevel(1);
            ucArea.setFullName(ucArea.getName());
            ucArea.setIdPath(ucArea.getId()+";");
        }else{
            ucArea.setLevel(ucAreaParent.getLevel()+1);
            ucArea.setFullName(ucAreaParent.getFullName()+ucArea.getName());
            ucArea.setIdPath(ucAreaParent.getIdPath()+ucArea.getId()+";");
        }

        baseMapper.updateById(ucArea);

        Map<String,Object> map = new HashMap<>();
        map.put("oldIdPath",olducArea.getIdPath());
        map.put("newIdPath",ucArea.getIdPath());
        map.put("oldFullName",olducArea.getFullName());
        map.put("newFullName",ucArea.getFullName());
        map.put("levelAdd",ucArea.getLevel()-olducArea.getLevel());
        map.put("level",olducArea.getLevel());

        baseMapper.updateByMap(map);
        return 1;
    }

    @Override
    public int delete(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public List<Long> findIdPath(Long id) {

        List<Long> ids = new ArrayList<>();
        ids.add(id);
        UcArea ucArea = baseMapper.selectById(id);
        int times = ucArea.getLevel()+1;
        for(int i=1;i<times;i++){
            if(ucArea.getParentId()!=0){
                ucArea = baseMapper.selectById(ucArea.getParentId());
                ids.add(ucArea.getId());
            }
        }

        Collections.reverse(ids);
        return ids;
    }
}
